import server from '@/utils/server'
import ServerConstants from '@/utils/ServerConstants'

export function getCode (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_SEND_VALIDATE_CODE, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function dologin (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_LOGIN, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function oauth (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_LOGIN_OAUTH, params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function getAddressList (success, errorfun) {
  return new Promise((resolve, reject) => {
    server.getJSON(ServerConstants.REQ_USER_GET_ADDRESS, {},
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function addAddress (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_ADD_ADDRESS,
      {data: server.encode(params)},
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}


export function del_address (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_DEL_ADDRESS,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

export function setDefaultAddress (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_DEFAULT_ADDRESS,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}



export function code2openId (params, success, errorfun) {
  if (server._isEmpty(params)) {
    params = {}
  }
  return new Promise((resolve, reject) => {
    server.postJSON(ServerConstants.REQ_USER_CODE_TO_OPENID,
      params,
      function (response) {
        resolve(response.data)
        server.call(success, response)
      }, function (error) {
        server.call(errorfun, error)
        reject(error)
      })
  })
}

